// 获取url参数
export function getParamsFromURL(url: any) {
  if ('undefined' == url) {
    return {};
  }
  const paraString = url.substring(url.indexOf('?') + 1, url.length).split('&');
  let paraObj: any = {};
  for (let i = 0; i < paraString.length; i++) {
    let j = paraString[i];
    paraObj[j.substring(0, j.indexOf('='))] = j.substring(j.indexOf('=') + 1, j.length);
  }
  return paraObj;
}

// url参数转对象
export const getParamsFromURLSearch = (): any => {
  var url = window.location.search;
  if (!url) {
    return {};
  }
  let paraString: string[] = url.substring(url.indexOf('?') + 1, url.length).split('&');
  let paraObj: any = {};
  for (var i = 0; i < paraString.length; i++) {
    var j = paraString[i];
    paraObj[j.substring(0, j.indexOf('='))] = j.substring(j.indexOf('=') + 1, j.length);
  }
  return paraObj;
};

//json转url参数
export const convertJsonToUrl = function (data: any) {
  var _result: any[] = [];
  for (var key in data) {
    var value = data[key];
    if (value.constructor == Array) {
      value.forEach(function (_value) {
        _result.push(key + '=' + _value);
      });
    } else {
      _result.push(key + '=' + value);
    }
  }
  return _result.join('&');
};

// 获取路由 pathname，因为独立项目使用自己的路由，乾坤使用父项目的路由（hash路由，多级路由拼起来）,lastLevelName不传时获取上一级路由prevPath
export const getPathname = (cur: string, lastLevelName?: string) => {
  let arr = cur.split('/');
  arr.pop();
  if (lastLevelName) {
    arr.push(lastLevelName);
  }
  return arr.join('/');
};
